package com.api.ittools.entity;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xcdh.commons.annotation.getter.XCDHGetter;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.Ognl;
import org.apache.ibatis.ognl.OgnlException;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

@Data
@Slf4j
@XCDHGetter(target = "data")
public class ITToolPage<T> extends Page<T> {
   private QueryWrapper<T> query ;
   private T data ;
   public ITToolPage() {
      super(0,20);
   }

   public void setData(T data){
      this.data = data ;
      query = new QueryWrapper<>(data) ;
   }

   public static boolean notNull(Object data){
      return data != null  ;
   }
   public  Object getValue(String property){
      if(data == null){
         return null ;
      }
      try {
         return Ognl.getValue(property , data) ;
      } catch (OgnlException e) {
         e.printStackTrace();
         return null ;
      }
   }
   public Object has(String property){
      Object value = getValue(property);
      if(value == null){
         return false ;
      }
      if(value instanceof String){
         return !isBlank(value.toString()) ;
      }
      return true ;
   }

   public static String trim(String val){
      return val.replaceAll("\\s","");
   }

   public static boolean isBlank(String val){
      return val == null || trim(val).length() == 0 ;
   }

}
